package com.campus.server.dao;

import com.campus.common.util.DBUtil;
import com.campus.common.vo.BorrowRecord;

import java.sql.*;
import java.util.*;
import java.util.Date;

public class BorrowRecordDao {

    public boolean addBorrowRecord(String username, int bookId, String title, String author, int timeLimit, Connection conn)
            throws SQLException {

        // 计算借书日期和归还期限
        java.util.Date currentDate = new java.util.Date();
        java.util.Date dueDate = new java.util.Date(currentDate.getTime() + timeLimit * 24L * 60 * 60 * 1000);

        // 修改SQL语句，添加time_limit列
        String sql = "INSERT INTO borrow_records (username, book_id, book_title, book_author, borrow_date, due_date, time_limit) " +
                "VALUES (?, ?, ?, ?, ?, ?, ?)"; // 添加time_limit占位符

        try (PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, username);
            stmt.setInt(2, bookId);
            stmt.setString(3, title);
            stmt.setString(4, author);
            stmt.setDate(5, new java.sql.Date(currentDate.getTime()));
            stmt.setDate(6, new java.sql.Date(dueDate.getTime()));
            stmt.setInt(7, timeLimit); // 设置time_limit值
            System.out.println("DEBUG: Adding borrow record, timeLimit=" + timeLimit);
            int rowsAffected = stmt.executeUpdate();
            return rowsAffected > 0;
        }
    }

    // 删除借阅记录 - 增加通过book_id删除的方法
    public boolean removeBorrowRecordByBookId(String username, int bookId) {
        System.out.println("DEBUG: 通过book_id删除借阅记录 - username: " + username + ", bookId: " + bookId);

        String sql = "DELETE FROM borrow_records WHERE username = ? AND book_id = ?";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setString(1, username);
            stmt.setInt(2, bookId);

            int rowsAffected = stmt.executeUpdate();
            System.out.println("DEBUG: 通过book_id删除影响行数: " + rowsAffected);
            return rowsAffected > 0;
        } catch (SQLException e) {
            System.out.println("DEBUG: 通过book_id删除异常: " + e.getMessage());
            e.printStackTrace();
            return false;
        }
    }

    public boolean removeBorrowRecord(String username, String title, String author) {
        System.out.println("DEBUG: 通过书名作者删除借阅记录 - username: " + username +
                ", title: " + title +
                ", author: " + author);

        String sql = "DELETE FROM borrow_records WHERE username = ? AND book_title = ? AND book_author = ?";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setString(1, username);
            stmt.setString(2, title);
            stmt.setString(3, author);

            int rowsAffected = stmt.executeUpdate();
            System.out.println("DEBUG: 通过书名作者删除影响行数: " + rowsAffected);
            return rowsAffected > 0;
        } catch (SQLException e) {
            System.out.println("DEBUG: 通过书名作者删除异常: " + e.getMessage());
            e.printStackTrace();
            return false;
        }
    }
    public List<String> getBorrowersByBookId(int bookId) {
        List<String> usernames = new ArrayList<>();
        String sql = "SELECT username FROM borrow_records WHERE book_id = ?";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setInt(1, bookId);
            ResultSet rs = stmt.executeQuery();

            while (rs.next()) {
                usernames.add(rs.getString("username"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return usernames;
    }
    public List<BorrowRecord> getBorrowRecordsByBookId(int bookId) {
        List<BorrowRecord> records = new ArrayList<>();
        // 添加book_title和book_author字段到查询中
        String sql = "SELECT username, book_title, book_author, borrow_date, due_date FROM borrow_records WHERE book_id = ? ";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setInt(1, bookId);
            ResultSet rs = stmt.executeQuery();

            while (rs.next()) {
                BorrowRecord record = new BorrowRecord();
                record.setUsername(rs.getString("username"));
                record.setBookTitle(rs.getString("book_title")); // 添加book_title
                record.setBookAuthor(rs.getString("book_author")); // 添加book_author
                record.setBorrowDate(rs.getDate("borrow_date"));
                record.setDueDate(rs.getDate("due_date"));
                records.add(record);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return records;
    }

    public List<BorrowRecord> getUserBorrowRecords(String username) {
        List<BorrowRecord> records = new ArrayList<>();
        String sql = "SELECT book_id, book_title, book_author, borrow_date, due_date FROM borrow_records WHERE username = ?";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setString(1, username);
            ResultSet rs = stmt.executeQuery();

            while (rs.next()) {
                BorrowRecord record = new BorrowRecord();
                record.setBookId(rs.getInt("book_id"));
                record.setBookTitle(rs.getString("book_title"));
                record.setBookAuthor(rs.getString("book_author"));
                record.setBorrowDate(rs.getDate("borrow_date"));
                record.setDueDate(rs.getDate("due_date"));
                records.add(record);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return records;
    }
}